home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter6 / makedl.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  7KB  |  244 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "makedl.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "MakeDragList()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LPCTSTR lpszData[8] = { "Apple", "Orange", "Pear", "Watermeleon", 
  108.                         "Grape", "Peach", "Strawberry", "Rasberry" };
  109.  
  110.  
  111. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  112. {
  113. static LPDRAGLISTINFO lpdli;
  114. static int  nItem;
  115. static int  nItemToMove = -1;
  116. static UINT uDLMsg      = 0;
  117. static HWND hList       = NULL;
  118.  
  119.    // Handle the dragging notifications.
  120.    //...................................
  121.    if ( uMsg == uDLMsg )
  122.    {
  123.       lpdli = (LPDRAGLISTINFO)lParam;
  124.  
  125.       switch (lpdli->uNotification) 
  126.       {
  127.          case DL_BEGINDRAG:
  128.                  nItemToMove = LBItemFromPt(lpdli->hWnd, lpdli->ptCursor, TRUE);
  129.                  return( TRUE );
  130.  
  131.          case DL_DRAGGING:
  132.                  nItem = LBItemFromPt(lpdli->hWnd, lpdli->ptCursor, TRUE);
  133.                  DrawInsert( hWnd, lpdli->hWnd, nItem);
  134.  
  135.                  if ( nItem != -1 ) 
  136.                  {
  137.                     SetCursor( LoadCursor( hInst, "DragCursor" ) ); 
  138.                     return( 0 );
  139.                  }
  140.  
  141.                  return( DL_STOPCURSOR );
  142.  
  143.          case DL_CANCELDRAG :
  144.                  nItemToMove = -1;
  145.                  break;
  146.  
  147.          case DL_DROPPED:
  148.                  nItem = LBItemFromPt(lpdli->hWnd, lpdli->ptCursor, TRUE);
  149.                  if ( nItem != -1 )
  150.                  {
  151.                     char szBuf[40];
  152.  
  153.                     SendMessage( hList, LB_GETTEXT, nItemToMove, (LPARAM)szBuf );
  154.                     SendMessage( hList, LB_DELETESTRING, nItemToMove, 0 );
  155.                     SendMessage( hList, LB_INSERTSTRING, nItem, (LPARAM)szBuf );
  156.                     SendMessage( hList, LB_SETCURSEL, nItem, 0 );
  157.                  }
  158.  
  159.                  DrawInsert(hWnd, lpdli->hWnd, -1);
  160.                  nItemToMove = -1;
  161.  
  162.                  return( DL_CURSORSET );
  163.       }
  164.    }
  165.  
  166.    switch( uMsg )
  167.    {
  168.       case WM_CREATE  :
  169.               InitCommonControls();
  170.  
  171.               hList = CreateWindow( "LISTBOX", "",
  172.                                      WS_CHILD | WS_VISIBLE | LBS_STANDARD | 
  173.                                      LBS_NOINTEGRALHEIGHT,
  174.                                      10, 100, 100, 100,
  175.                                      hWnd,
  176.                                      (HMENU)1,
  177.                                      hInst,
  178.                                      NULL);
  179.               if ( hList )
  180.               {
  181.                  int i;
  182.  
  183.                  for( i=0; i<8; i++ )
  184.                     SendMessage( hList, LB_INSERTSTRING, (WPARAM)-1, 
  185.                                                          (LPARAM)lpszData[i] );
  186.  
  187.                  MakeDragList( hList );
  188.                  uDLMsg = RegisterWindowMessage( DRAGLISTMSGSTRING );
  189.               }
  190.               break;
  191.  
  192.       case WM_SIZE :
  193.               if ( hList )
  194.                  MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  195.               break;
  196.  
  197.       case WM_COMMAND :
  198.               switch( LOWORD( wParam ) )
  199.               {
  200.                  case IDM_ABOUT :
  201.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  202.                         break;
  203.  
  204.                  case IDM_EXIT :
  205.                         DestroyWindow( hWnd );
  206.                         break;
  207.               }
  208.               break;
  209.       
  210.       case WM_DESTROY :
  211.               PostQuitMessage(0);
  212.               break;
  213.  
  214.       default :
  215.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  216.    }
  217.  
  218.    return( 0L );
  219. }
  220.  
  221.  
  222. LRESULT CALLBACK About( HWND hDlg,           
  223.                         UINT message,        
  224.                         WPARAM wParam,       
  225.                         LPARAM lParam)
  226. {
  227.    switch (message) 
  228.    {
  229.        case WM_INITDIALOG: 
  230.                return (TRUE);
  231.  
  232.        case WM_COMMAND:                              
  233.                if (   LOWORD(wParam) == IDOK         
  234.                    || LOWORD(wParam) == IDCANCEL)    
  235.                {
  236.                        EndDialog(hDlg, TRUE);        
  237.                        return (TRUE);
  238.                }
  239.                break;
  240.    }
  241.  
  242.    return (FALSE); 
  243. }
  244.